// pages/index/serve.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    min: 0,
    checked: 1,
    zhankai: false,
    pics: [],
    daijiesuanshow: true,
    shouhuanmalod: false,
    max: 200,
    disabled: true,
    array: ['美国', '中国', '巴西', '日本'],
    arrayindex: 0,
    arrayindex1: 0,
    array1: ['美国', '中国', '巴西', '日本'],
    date: {
      date: '2019-01-01 13:37',
      startDate: '2019-01-01 12:37',
      endDate: '2019-03-12 12:38',
      placeholder: '请选择时间',
    },
    date1: {
      date: '2019-01-01 13:37',
      startDate: '2019-01-01 12:37',
      endDate: '2019-03-12 12:38',
      placeholder: '请选择时间',
    },
    pageName: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      pageName: options.name
    })
  },
  navbind(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      navindex: index
    })
  },
  onPickerChange(e) {
    let date = this.data.date
    date.dateString = e.detail.dateString
    this.setData({
      date: date
    })
  },
  onPickerChange1(e) {
    let date1 = this.data.date1
    date1.dateString = e.detail.dateString
    this.setData({
      date1: date1
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      arrayindex: e.detail.value
    })
  },
  bindPickerChange1: function (e) {
    this.setData({
      arrayindex1: e.detail.value
    })
  },
  btn(e) {
    let name = e.currentTarget.dataset.name;
    let disabled = this.data.disabled
    if (name == 'wangon') {
      wx.showModal({
        content: '请确认质检完成，可以完工了？',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else if (name == 'genduo') {
      wx.showActionSheet({
        itemList: ['签名', '修改'],
        success: function (res) {
          if(res.tapIndex == 0) {
            wx.navigateTo({
              url: '/pages/sign/sign'
            })
          }
          if(res.tapIndex == 1) {
            wx.showToast({
              title: '数据可编辑',
              icon: 'success',
              duration: 800
            })
            disabled = !disabled
            that.setData({
              disabled: disabled
            })
          }
        }
      })
    } else if (name == 'xiugai') {
      if (disabled) {
        this.setData({
          disabled: false
        })
        wx.showToast({
          title: '数据可编辑',
          icon: ''
        })
      } else {
        this.setData({
          disabled: true
        })
        wx.showToast({
          title: '数据不可编辑',
          icon: ''
        })
      }
    }
  },
  radioChange(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      checked: index
    })
  },  
  show() {
    this.setData({
      show: false
    })
  },
  hide() {
    this.setData({
      show: true
    })
  },
  inputs: function (e) {
    var value = e.detail.value;
    var len = parseInt(value.length);
    if (len > this.data.max) return;

    this.setData({
      currentWordNumber: len
    });
    if (this.data.currentWordNumber == 200) {
      wx.showModal({
        title: '提示',
        content: '您输入的次数已达上限',
      })
    }
  },
  previewImg: function (e) {

    var index = e.target.dataset.index;//当前图片地址
    var imgArr = e.target.dataset.list;//所有要预览的图片的地址集合 数组形式
    console.log(index, imgArr)
    wx.previewImage({
      current: imgArr[index],
      urls: imgArr,
    })
  },
  //上传图片开始
  chooseImg: function (e) {
    var that = this, pics = this.data.pics;
    if (pics.length < 3) {
      wx.chooseImage({
        count: 3, // 最多可以选择的图片张数，默认9
        sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
        sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          for (var i = 0; i < tempFilePaths.length; i++) {
            pics.push(tempFilePaths[i]);
          }
          that.setData({
            pics: pics
          })
        },
      });
    } else {
      wx.showToast({
        title: '最多上传3张图片',
        icon: 'none',
        duration: 3000
      });

    }
  },
  // 删除图片
  deleteImg: function (e) {
    var that = this;
    var pics = this.data.pics;
    var index = e.currentTarget.dataset.index;
    pics.splice(index, 1);
    console.log(pics)
    this.setData({
      pics: pics,
    })
  },
  // 预览图片
  previewImg1: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var pics = this.data.pics;
    wx.previewImage({
      //当前显示图片
      current: pics[index],
      //所有图片
      urls: pics
    })
  },
  daijiesuanshow() {
    this.setData({
      daijiesuanshow: false
    })
  },
  daijiesuanhide() {
    this.setData({
      daijiesuanshow: true
    })
  },
  shouhuanmalod() {
    this.setData({
      shouhuanmalod: true
    })
  },
  noshouhuanmalod() {
    this.setData({
      shouhuanmalod: false
    })
  },
  zhankai() {
    let zhankai = this.data.zhankai
    zhankai = !zhankai
    this.setData({
      zhankai: zhankai
    })
  }
})